//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory 2010
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// This class is the concrete implementation of a View in the Model-View-Presenter 
// pattern. Communication between the Presenter and this class is acheived through 
// an interface to facilitate separation and testability.
// Note that the Presenter generated by the same recipe, will automatically be created
// by CAB through [CreateNew] and bidirectional references will be added.
//
// For more information see:
// ms-help://MS.VSCC.v80/MS.VSIPCC.v80/ms.scsf.2006jun/SCSF/html/03-030-Model%20View%20Presenter%20%20MVP%20.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using GlobalBank.Infrastructure.Interface.BusinessEntities;
using GlobalBank.Infrastructure.Interface.Services;
using GlobalBank.Infrastructure.UI;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;

namespace GlobalBank.BranchSystems.Module.Views
{
	[SmartPart]
	public partial class AddVisitorToQueueView : UserControl, IAddVisitorToQueueView, ISmartPartInfoProvider
	{
		public AddVisitorToQueueView()
		{
			InitializeComponent();
		}

		private IMessageBoxService _messageBoxService;

		/// <summary>
		/// Sets the presenter. The dependency injection system will automatically
		/// create a new presenter for you.
		/// </summary>
		[CreateNew]
		public AddVisitorToQueueViewPresenter Presenter
		{
			set
			{
				_presenter = value;
				_presenter.View = this;
			}
		}

		[ServiceDependency]
		public IMessageBoxService MessageBoxService
		{
			set { _messageBoxService = value; }
		}

		protected override void OnLoad(EventArgs e)
		{
			_presenter.OnViewReady();
		}

		public void ShowCustomer(Customer customer)
		{
			UpdateControlsToWorkingMode(AddVisitorToQueueWorkingMode.AddCustomer);
			ShowPerson(customer);
		}

		public void ShowWalkin(WalkIn walkIn)
		{
			UpdateControlsToWorkingMode(AddVisitorToQueueWorkingMode.AddVisitor);
			ShowPerson(walkIn);
		}

		public void NotifyCustomerAlreadyInQueue()
		{
			_messageBoxService.Show(Properties.Resources.CustomerAlreadyInQueueMessage,
			                        Properties.Resources.CustomerAlreadyInQueueTitle);
		}

		protected virtual void ShowPerson(Person person)
		{
			_personBindingSource.DataSource = person;
			_addressBindingSource.DataSource = person.Addresses[0];
			_emailAddressBindingSource.DataSource = person.EmailAddresses[0];
			foreach (PhoneNumber phone in person.PhoneNumbers)
			{
				if (phone.PhoneType == PhoneType.Home)
					_homePhoneNumberBindingSource.DataSource = phone;
				if (phone.PhoneType == PhoneType.Work)
					_workPhoneNumberBindingSource.DataSource = phone;
				if (phone.PhoneType == PhoneType.Mobile)
					_mobilePhoneNumberBindingSource.DataSource = phone;
			}
		}

		protected virtual void UpdateControlsToWorkingMode(AddVisitorToQueueWorkingMode workingMode)
		{
			foreach (Control ctrl in Controls)
			{
				TextBox textBox = ctrl as TextBox;
				if (textBox != null)
				{
					if (workingMode == AddVisitorToQueueWorkingMode.AddCustomer)
					{
						textBox.BorderStyle = BorderStyle.None;
						textBox.ReadOnly = true;
					}
					else
					{
						textBox.BorderStyle = BorderStyle.Fixed3D;
						textBox.ReadOnly = false;
					}
				}
			}

			if (workingMode == AddVisitorToQueueWorkingMode.AddCustomer)
			{
				_descriptionTextBox.ReadOnly = false;
				_descriptionTextBox.BorderStyle = BorderStyle.Fixed3D;
				_firstNameRequiredLabel.Visible = false;
				_lastNameRequiredLabel.Visible = false;
			}
		}

		public ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
		{
			ISmartPartInfo spi = null;

			if (smartPartInfoType.IsAssignableFrom(typeof (WindowSmartPartInfo)))
			{
				WindowSmartPartInfo wspi = new WindowSmartPartInfo();
				wspi.Modal = true;
				wspi.MinimizeBox = false;
				wspi.MaximizeBox = false;
				wspi.ControlBox = true;

				wspi.Keys[WindowWorkspaceSetting.FormBorderStyle] = FormBorderStyle.FixedDialog;
				wspi.Keys[WindowWorkspaceSetting.CancelButton] = _cancelButton;
				wspi.Keys[WindowWorkspaceSetting.AcceptButton] = _okButton;

				spi = wspi;
			}
			else
			{
				spi = Activator.CreateInstance(smartPartInfoType) as ISmartPartInfo;
			}

			spi.Description = Properties.Resources.FindCustomerResultsViewDescription;

			if (_presenter.WorkingMode == AddVisitorToQueueWorkingMode.AddCustomer)
			{
				spi.Title = Properties.Resources.AddReasonTitle;
			}
			else
			{
				spi.Title = Properties.Resources.AddVisitorTitle;
			}
			return spi;
		}

		private void _okButton_Click(object sender, EventArgs e)
		{
			_presenter.QueueForService(_reasonCodeComboBox.Text, _descriptionTextBox.Text);
		}

		private void UpdateButtons()
		{
			bool enabled = (_firstNameTextBox.Text.Length > 0 && _lastNameTextBox.Text.Length > 0) &&
			               (_reasonCodeComboBox.Text.Length > 0);

			_okButton.Enabled = enabled;
		}

		private void _reasonCodeComboBox_TextChanged(object sender, EventArgs e)
		{
			UpdateButtons();
		}

		private void _descriptionTextBox_TextChanged(object sender, EventArgs e)
		{
			UpdateButtons();
		}

		private void _lastNameTextBox_TextChanged(object sender, EventArgs e)
		{
			UpdateButtons();
		}

		private void _firstNameTextBox_TextChanged(object sender, EventArgs e)
		{
			UpdateButtons();
		}

		private void _firstNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _middleInitialTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _lastNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidAlphaChar(e.KeyChar);
		}

		private void _streetTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidGeneralChar(e.KeyChar);
		}

		private void _cityTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidGeneralChar(e.KeyChar);
		}

		private void _zipTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidNumeralChar(e.KeyChar);
		}

		private void _homeNumberTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidNumeralDashChar(e.KeyChar);
		}

		private void _workNumberTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidNumeralDashChar(e.KeyChar);
		}

		private void _cellNumberTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidNumeralDashChar(e.KeyChar);
		}

		private void _ssnTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidNumeralDashChar(e.KeyChar);
		}

		private void _emailTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			e.Handled = !InputValidation.IsValidEmailChar(e.KeyChar);
		}
	}
}
